<?php

namespace model\admin;
use model;
class orderModel extends model
{

    public static function get_list( $filter = [] ){

        $sql = 'select * from sh_order';

        $query = self::$db->query( $sql );

        return $query->rows;

    }

    public static function get_order( $order_id ){

        $sql = 'select * from sh_order where id=' . $order_id;

        $query = self::$db->query( $sql );
        $order = $query->row;

        $order['goods'] = self::get_order_goods( $order_id );
        $order['total'] = self::get_order_total( $order_id );
        $order['history_list'] = self::history_list( $order_id );
        return $order;

    }

    public static function get_order_goods( $order_id ){

        $sql = 'select * from sh_order_goods where order_id=' . $order_id;
        $query = self::$db->query( $sql );

        return $query->rows;
    }

    public static function get_order_total( $order_id ){

        $sql = 'select * from sh_order_total where order_id=' . $order_id;
        $query = self::$db->query( $sql );

        return $query->rows;

    }

    public static function get_all_status(){

        $sql = "SELECT * FROM sh_order_status WHERE is_active = 1;";
        $query = self::$db->query( $sql );

        return $query->rows;
    }

    public static function add_history( $order_id, $data ){

        $sql = "INSERT INTO sh_order_history SET order_id = '" . $order_id . "',
                `comment` = '" . $data['comment'] . "',
                order_status = '" . $data['order_status'] . "',
                create_time = '" . $data['create_time'] . "'";
        $query = self::$db->query( $sql );


        //todo 修改订单状态
        $change_sql = "UPDATE sh_order SET status= '" . $data['order_status'] . "' WHERE id= " . $order_id;
        self::$db->query( $change_sql );

        return $query->num_rows;

    }

    public static function history_list( $order_id ){

        $sql = 'SELECT 
              h.*,
              s.`name`
            FROM
              sh_order_history h 
              LEFT JOIN sh_order_status s 
                ON h.`order_status` = s.`code` where h.order_id=' . $order_id . ' order by h.create_time desc';

        $query = self::$db->query( $sql );

        return $query->rows;
    }
}